using System.Collections;
using Concurso.Clases;
using Concurso.Clases.TreeViewManage;
using ConcursoCV.UI.Clases;
using Curriculum.Metadatos.ClasesMetadatos;
using Curriculum.Modelo.Clases;
using Curriculum.Services;
using FunkeLab;

namespace ConcursoCV.UI.WebUserControls.Concurso
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ArbolCalificaciones.
	/// </summary>
	public class ArbolCalificaciones : TreeViewControl
	{
		protected FunkeLab.MyTreeView treeView;
		protected System.Web.UI.WebControls.Label lblError;

		private const char SEPARATOR = ':';

		#region - Public Events
		
		// Tree
		public event EventHandler TreeViewNodeClick;
		public event EventHandler TreeViewNodeOnDemand;
		public event EventHandler TreeViewOnNodeClose;

		#endregion - Public Events

		private void Page_Load(object sender, System.EventArgs e)
		{
			lblError.Text = string.Empty;
			MyTreeView.ImagesFolder = "img/tree/";			
			if(!this.IsPostBack)
			{
				this.DocenteId = this.Request["docenteId"];
				InitalizeTreeView();
				if( this.Request["type"] != null )
					if( this.Request["type"] == "DOC")
					{
						this.DocenteId = Helper.SecurityHelper.GetUsuarioID();
					}
			}
		}
		private IList GetRootNodes()
		{
			VersionMetadatosService svc = new VersionMetadatosService();
			IList rootNodes = svc.ObtenerNodosRaiz();
			return rootNodes;
		}

		protected void LoadNodes( MyTreeViewItem parentItem, IList nodes, bool loadOnExpand, NodeType nodeType) 
		{
			if( nodeType == NodeType.METADATO )
			{
				foreach( AntecedenteMetadato node in nodes ) 
				{
					MyTreeViewItem item = new MyTreeViewItem();
					item.Text = node.Nombre;
					item.Value = node.Id + SEPARATOR + NodeTypeConverter.ToString( nodeType );
					item.LoadOnExpand = loadOnExpand;
					parentItem.Items.Add( item );
				}				
			}
			else
			{
				int i = 1;
				foreach( Antecedente node in nodes ) 
				{
					MyTreeViewItem item = new MyTreeViewItem();
					item.Text = "Antecedente " + i.ToString();
					item.Value = node.Id + SEPARATOR + NodeTypeConverter.ToString( nodeType );
					item.LoadOnExpand = loadOnExpand;
					parentItem.Items.Add( item );
					i++;
				}								
			}
		}

		public void InitalizeTreeView() 
		{
			RootItem.Items.Clear();
			IList rootNodes = GetRootNodes();				
			LoadNodes( RootItem, rootNodes, true , NodeType.METADATO);
		}

		private NodeType GetNodeType( string itemValue )
		{
			return (NodeType)System.Enum.Parse(typeof(NodeType), itemValue.Split(SEPARATOR)[1]);			
		}

		#region - PaintSelectedNode -


		public void UndoPaintSelectedNode(ArrayList path)
		{
			treeView.GetNode(path).BackColor = System.Drawing.Color.White;
		}

		#endregion - PaintSelectedNode -

		#region - properties -

		protected override MyTreeViewItem RootItem 
		{
			get {return treeView.Items[0];}
		}

		private string DocenteId
		{
			get{ return (string) ViewState["_docenteId"];}	
			set{ ViewState["_docenteId"] = value;}			
		}

		public string ErrorMessage
		{
			get{ return lblError.Text;}
			set{ lblError.Text = value;}		
		}
	
		public override MyTreeView Tree
		{
			get
			{
				return this.treeView;
			}
		}

		#endregion - properties -

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			MyInitializeComponent();
			base.OnInit(e);
		}
		private void MyInitializeComponent()
		{
			this.treeView.OnNodeClick +=new FunkeLab.MyTreeView.OnNodeClickEventHandler(treeView_OnNodeClick);
			this.treeView.OnNodeClose +=new FunkeLab.MyTreeView.OnNodeCloseEventHandler(treeView_OnNodeClose);
			this.treeView.OnNodeLoadOnDemand +=new FunkeLab.MyTreeView.OnNodeLoadOnDemandEventHandler(treeView_OnNodeLoadOnDemand);
		}		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		protected override void treeView_OnNodeClick(ref MyTreeViewItem Item)
		{
			if( Item.Value != RootItem.Value)
			{
				NodeType nodeType = GetNodeType( Item.Value );
				if( nodeType != NodeType.METADATO )
				{
					PaintSelectedNode( treeView,Item );		
				}
				else
				{
					Item = null;
					lblError.Text = "Seleccione un antecedente";
				}
				this.RaiseTreeViewNodeClickEvent( Item );
			}		
		}

		private void treeView_OnNodeClose(ref MyTreeViewItem Item)
		{
			if ( !( Item.Value == RootItem.Value )) 
			{
				Item.Items.Clear();
			}
			else
			{
				if(SelectedPathNode != null) 
					UndoPaintSelectedNode(SelectedPathNode);
			}

			this.RaiseTreeViewOnNodeClose();		
		}

		protected override void treeView_OnNodeLoadOnDemand(ref MyTreeViewItem Item)
		{
			string nodeId = string.Empty;
			NodeType nodeType;
			IList nodes = null;
			ErrorMessage = string.Empty;			
			if( Item != RootItem )
			{
				nodeId = Item.Value.Split(SEPARATOR)[0];
				bool loadOnExpand = true;
				ConcursoService service = new ConcursoService();
				if( service.EsHoja(nodeId) )
				{					
					ConcursoService svc = new ConcursoService();				
					nodes = svc.GetAntecedentesHojaPorTipoyDocente(nodeId, DocenteId);
					loadOnExpand = false;
					nodeType = NodeType.ANTECEDENTE;
				}
				else
				{
					VersionMetadatosService svc = new VersionMetadatosService();
					nodes = svc.ObtenerNodosHijo(nodeId);
					nodeType = NodeType.METADATO;
				}
				if(nodes.Count>0)
				{
					LoadNodes( Item , nodes, loadOnExpand, nodeType);
				}									
			}
			this.RaiseTreeViewNodeOnDemandEvent( Item );
		}

		#region - RaiseEvents -

		protected void RaiseTreeViewNodeClickEvent ( MyTreeViewItem item ) 
		{
			if (this.TreeViewNodeClick != null)
				this.TreeViewNodeClick(this, new TreeViewEventArgs(item) );		
		}

		protected void RaiseTreeViewNodeOnDemandEvent (MyTreeViewItem node) 
		{
			if (this.TreeViewNodeOnDemand != null)
				this.TreeViewNodeOnDemand(this, null);		
		}

		protected void RaiseTreeViewOnNodeClose () 
		{
			if (this.TreeViewOnNodeClose != null)
				this.TreeViewOnNodeClose(this, null);		
		}
		
		#endregion - RaiseEvents -
	}
}
